package org.xqp.pet.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xqp.basic.exception.MyException;
import org.xqp.basic.util.JsonResult;
import org.xqp.basic.util.LoginContextUtil;
import org.xqp.basic.util.PageList;
import org.xqp.pet.domain.Pet;
import org.xqp.pet.domain.SearchMasterMsg;
import org.xqp.pet.query.SearchMasterMsgQuery;
import org.xqp.pet.service.ISearchMasterMsgService;
import org.xqp.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 官方说法是发布宠物寻主
 * 实际上就是用户发布消息卖宠物
 */

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;

    /**
     * 宠物寻主
     * @param smm
     * @return
     */
    @PostMapping("publish")
    public JsonResult publish(@RequestBody SearchMasterMsg smm,HttpServletRequest request){
        try {
            // 获取到储存在请求头中的用户信息
            Logininfo logininfo= LoginContextUtil.getLoginContext(request);
            // 获取到请求头中登录对象的id
            Long logininfoId = logininfo.getId();
            // 调用publish方法并将smm和logininfoId传入
            seachMasterMsgService.publish(smm,logininfoId);
            return JsonResult.me();
        } catch (MyException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙");
        }
    }

    /**
     * 待处理的发布的宠物买卖信息
     * @param
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            // 获取请求头中的登录对象信息
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);

            return  seachMasterMsgService.pending(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 已处理的发布的宠物买卖信息
     * @param searchMasterMsgQuery
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request){
        try {
            // 获取请求头中的登录对象信息
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            return  seachMasterMsgService.processed(searchMasterMsgQuery,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 处理发布的宠物寻主信息
     * @return
     */
    @PutMapping("/handle")
    public JsonResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            seachMasterMsgService.handler(pet,logininfo.getId());
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常");
        }
    }
}
